﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Brilliants.UI
{
    public class NbTabControl : TabControl
    {
        static NbTabControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbTabControl), new FrameworkPropertyMetadata(typeof(NbTabControl)));
        }
        public NbTabControl()
        {
            //this.SetResourceReference(NbTabControl.StyleProperty, nameof(NbTabControl));
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NbTabItem();
        }
    }


    public class NbTabItem : TabItem
    {
        static NbTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbTabItem), new FrameworkPropertyMetadata(typeof(NbTabItem)));
        }
        public NbTabItem()
        {
            //this.SetResourceReference(NbTabItem.StyleProperty, nameof(NbTabItem));
        }

        /// <summary>
        /// 选中背景色
        /// </summary>
        public SolidColorBrush SelectedColor
        {
            get { return (SolidColorBrush)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(SolidColorBrush), typeof(NbTabItem), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 124, 125, 133))));

        /// <summary>
        /// 选中文字颜色
        /// </summary>
        public SolidColorBrush SelectedForeground
        {
            get { return (SolidColorBrush)GetValue(SelectedForegroundProperty); }
            set { SetValue(SelectedForegroundProperty, value); }
        }
        public static readonly DependencyProperty SelectedForegroundProperty =
            DependencyProperty.Register("SelectedForeground", typeof(SolidColorBrush), typeof(NbTabItem), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 229, 229, 231))));


        /// <summary>
        /// 显示关闭按钮
        /// </summary>
        public Visibility ShowCloseButton
        {
            get { return (Visibility)GetValue(ShowCloseButtonProperty); }
            set { SetValue(ShowCloseButtonProperty, value); }
        }
        public static readonly DependencyProperty ShowCloseButtonProperty =
            DependencyProperty.Register("ShowCloseButton", typeof(Visibility), typeof(NbTabItem), new PropertyMetadata(Visibility.Collapsed));



        /// <summary>
        /// 关闭TabItem
        /// </summary>
        //public static ICommand NbTabItemCloseCommand
        //{
        //    get
        //    {
        //        return new DelegateCommand(obj =>
        //        {
        //            var tabItem = obj as NbTabItem;
        //            var tabControl = FindParentTabControl(tabItem);
        //            if (tabControl != null)
        //                tabControl.Items.Remove(tabItem);
        //        });
        //    }
        //}

        /// <summary>
        /// 递归找父级TabControl
        /// </summary>
        /// <param name="reference">依赖对象</param>
        /// <returns>TabControl</returns>
        private static NbTabControl FindParentTabControl(DependencyObject reference)
        {
            DependencyObject dObj = VisualTreeHelper.GetParent(reference);
            if (dObj == null)
                return null;
            if (dObj.GetType() == typeof(NbTabControl))
                return dObj as NbTabControl;
            else
                return FindParentTabControl(dObj);
        }
    }



    public class NbMenuTabControl : TabControl
    {
        static NbMenuTabControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbMenuTabControl), new FrameworkPropertyMetadata(typeof(NbMenuTabControl)));
        }
        public NbMenuTabControl()
        {
            //this.SetResourceReference(NbMenuTabControl.StyleProperty, nameof(NbMenuTabControl));
        }
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NbMenuTabItem();
        }
    }


    public class NbMenuTabItem : TabItem
    {
        static NbMenuTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbMenuTabItem), new FrameworkPropertyMetadata(typeof(NbMenuTabItem)));
        }
        public NbMenuTabItem()
        {
            //this.SetResourceReference(NbMenuTabItem.StyleProperty, nameof(NbMenuTabItem));
        }

        
    }


    public class NbLineTabControl : TabControl
    {
        static NbLineTabControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbLineTabControl), new FrameworkPropertyMetadata(typeof(NbLineTabControl)));
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NbLineTabItem();
        }
    }


    public class NbLineTabItem : TabItem
    {
        static NbLineTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbLineTabItem), new FrameworkPropertyMetadata(typeof(NbLineTabItem)));
        }
    }

    public class NbBoxTabControl : TabControl
    {
        static NbBoxTabControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbBoxTabControl), new FrameworkPropertyMetadata(typeof(NbBoxTabControl)));
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NbBoxTabItem();
        }

        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);

            foreach (var item in this.Items)
            {
                var ni = item as NbBoxTabItem;
                if (ni == null) continue;
                ni.Index = this.Items.IndexOf(ni);
                ni.IsLeft = ni.Index == 0;
                ni.IsRight = ni.Index == this.Items.Count - 1;
            }
        }
    }


    public class NbBoxTabItem : TabItem
    {
        static NbBoxTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbBoxTabItem), new FrameworkPropertyMetadata(typeof(NbBoxTabItem)));
        }

        protected override void AddChild(object value)
        {
            base.AddChild(value);
        }

        public int Index
        {
            get { return (int)GetValue(IndexProperty); }
            set { SetValue(IndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Index.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(NbBoxTabItem), new PropertyMetadata(0));



        public bool IsLeft
        {
            get { return (bool)GetValue(IsLeftProperty); }
            set { SetValue(IsLeftProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsLeft.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsLeftProperty =
            DependencyProperty.Register("IsLeft", typeof(bool), typeof(NbBoxTabItem), new PropertyMetadata(false));



        public bool IsRight
        {
            get { return (bool)GetValue(IsRightProperty); }
            set { SetValue(IsRightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsRight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsRightProperty =
            DependencyProperty.Register("IsRight", typeof(bool), typeof(NbTabItem), new PropertyMetadata(false));


    }
}
